<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户编号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目标ID" prop="targetId">
        <el-input
          v-model="queryParams.targetId"
          placeholder="请输入目标ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="info" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['blog:article_interactions:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['blog:article_interactions:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['blog:article_interactions:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['blog:article_interactions:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="article_interactionsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="互动编号" align="center" prop="id" />
      <el-table-column label="用户编号" align="center" prop="userId" />
      <el-table-column label="互动目标类型" align="center" prop="targetType" >
        <template #default="scope">
          <div
            v-for="dict in blog_target_type" 
            :key="dict.value" 
          >
            <el-tag v-if="scope.row.targetType == dict.value">
              {{dict.label}}
            </el-tag> 
          </div>
        </template>
      </el-table-column>
      <el-table-column label="目标编号" align="center" prop="targetId" />
      <el-table-column label="互动类型" align="center" prop="actionType" >
        <template #default="scope">
          <div
            v-for="dict in blog_interaction_type" 
            :key="dict.value" 
          >
            <el-tag v-if="scope.row.actionType == dict.value">
              {{dict.label}}
            </el-tag> 
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="info"  @click="handleUpdate(scope.row)" v-hasPermi="['blog:article_interactions:edit']">修改</el-button>
          <el-button link type="info"  @click="handleDelete(scope.row)" v-hasPermi="['blog:article_interactions:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改互动管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="article_interactionsRef" :model="form" :rules="rules" label-width="80px">
       
        <el-form-item label="目标ID" prop="targetId">
          <el-input v-model="form.targetId" placeholder="请输入目标ID" />
        </el-form-item>
        <el-form-item label="互动目标类型" prop="targetType">
          <!-- 选择框 -->
           <el-select v-model="form.targetType" placeholder="请选择互动目标类型">
              <el-option
                v-for="dict in blog_target_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
           </el-select>
        </el-form-item>
        <el-form-item label="互动类型" prop="targetType">
          <!-- 选择框 -->
           <el-select v-model="form.actionType" placeholder="请选择互动类型">
              <el-option
                v-for="dict in blog_interaction_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
           </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Article_interactions">
import { listArticle_interactions, getArticle_interactions, delArticle_interactions, addArticle_interactions, updateArticle_interactions } from "@/api/blog/article_interactions";

const { proxy } = getCurrentInstance();
const { blog_interaction_type,blog_target_type } = proxy.useDict("blog_interaction_type","blog_target_type");

const article_interactionsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    targetType: null,
    targetId: null,
    actionType: null,
    createTime: null
  },
  rules: {
    userId: [
      { required: true, message: "用户ID，关联用户表 users.id不能为空", trigger: "blur" }
    ],
    targetType: [
      { required: true, message: "互动目标类型，可以是文章或评论不能为空", trigger: "change" }
    ],
    targetId: [
      { required: true, message: "目标ID，表示具体的文章或评论的ID不能为空", trigger: "blur" }
    ],
    actionType: [
      { required: true, message: "互动类型，可以是点赞、收藏或分享不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询互动管理列表 */
function getList() {
  loading.value = true;
  listArticle_interactions(queryParams.value).then(response => {
    article_interactionsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    targetType: null,
    targetId: null,
    actionType: null,
    createTime: null
  };
  proxy.resetForm("article_interactionsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加互动管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getArticle_interactions(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改互动管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["article_interactionsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateArticle_interactions(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addArticle_interactions(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除互动管理编号为"' + _ids + '"的数据项？').then(function() {
    return delArticle_interactions(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('blog/article_interactions/export', {
    ...queryParams.value
  }, `article_interactions_${new Date().getTime()}.xlsx`)
}

getList();
</script>
